#!/usr/bin/env bash

# pid file
PID_FILE_NAME="pid_file"

# file to upload
EXECUTABLE_JAR_NAME="script-1.0.jar"
TMP_EXECUTABLE_JAR_NAME=${EXECUTABLE_JAR_NAME}".tmp"
BAK_EXECUTABLE_JAR_NAME=${EXECUTABLE_JAR_NAME}".bak"
EXECUTABLE_JAR_PATH="./target/${EXECUTABLE_JAR_NAME}"

DEPLOY_SCRIPT="deploy.sh"
START_FUNC_NAME="start"

#env, ssh remote, work dir
ENV_PRO="pro"
SSH_HOST_PRO=("finbtc@47.89.20.254")
WORK_DIR_PRO="/home/finbtc/workspace/market_center_script/"
RUM_PARAM_PRO=" --env pro "

# ssh remote
ENV_TEST="test"
SSH_HOST_TEST=("finbtc@192.168.0.202")
WORK_DIR_TEST="/home/finbtc/workspace/market_center_script/"
RUM_PARAM_TEST=" --env dev "

# log file
declare LOG_FILES
LOG_BACKUP_FOLDER="logs/"

function stop() {
    # kill
    if [ -f "${PID_FILE_NAME}" ]; then
        pid=$(cat ${PID_FILE_NAME})
        kill -9 ${pid}
    fi
}

function start() {
    work_dir=`dirname $0`
    cd ${work_dir}

    stop

    mv ${EXECUTABLE_JAR_NAME} ${BAK_EXECUTABLE_JAR_NAME}
    mv ${TMP_EXECUTABLE_JAR_NAME} ${EXECUTABLE_JAR_NAME}

    chmod 755 ${EXECUTABLE_JAR_NAME}
    # run
    nohup java -server -jar ${EXECUTABLE_JAR_NAME} "$@" > /dev/null 2>&1 &
    echo $! > ${PID_FILE_NAME}
}

function compile() {
    mvn clean package spring-boot:repackage -Dmaven.test.skip=true
    ret=$?
    if [[ ${ret} -ne 0 ]] ; then
        return 1
    fi
}

function upload() {
    # upload jar
    # rename to *.jar.bak
    scp ${EXECUTABLE_JAR_PATH} $1:$2${TMP_EXECUTABLE_JAR_NAME}
    ret=$?
    if [[ ${ret} -ne 0 ]] ; then
        echo 'Failed to scp jar'
        return 1
    fi

    # upload script
    scp ${DEPLOY_SCRIPT} $1:$2
    ret=$?
    if [[ ${ret} -ne 0 ]] ; then
        echo 'Failed to scp script'
        return 1
    fi
}

function deploy() {
    # package
    echo "*******Start to package*******"
    compile
    ret=$?
    if [[ ${ret} -ne 0 ]] ; then
        echo 'Failed to compile'
        exit ${ret}
    fi

    if [ "$1" = "${ENV_PRO}" ]; then
        SSH_HOST=${SSH_HOST_PRO[@]}
        WORK_DIR=${WORK_DIR_PRO}
        RUN_PARAM=${RUM_PARAM_PRO}
    elif [ "$1" = "${ENV_TEST}" ]; then
        SSH_HOST=${SSH_HOST_TEST[@]}
        WORK_DIR=${WORK_DIR_TEST}
        RUN_PARAM=${RUM_PARAM_TEST}
    else
        echo "Unknown env: $1"
        exit
    fi

    for host in ${SSH_HOST[@]}
    do
        # upload jar and deploy.sh
        echo "*******Start to upload:${host} *******"
        upload ${host} ${WORK_DIR}
        ret=$?
        if [[ ${ret} -ne 0 ]] ; then
            echo 'Failed to upload files'
            exit ${ret}
        fi
    done

    for host in ${SSH_HOST[@]}
    do
        # run
        echo "*******Start service:${host} *******"
        ssh ${host} "bash ${WORK_DIR}${DEPLOY_SCRIPT} ${START_FUNC_NAME} ${RUN_PARAM}"
        echo "*******Done*******"
    done
}

if [ "$1" = "${START_FUNC_NAME}" ]; then
    start "$@"
elif [ "$1" = "${ENV_TEST}" ]; then
    deploy $1
elif [ "$1" = "${ENV_PRO}" ]; then
    deploy $1
elif [ "$1" = "stop" ]; then
    stop
else
    echo "miss argument!"
fi

